package com.example.timmy.myapplication.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.timmy.myapplication.R;
import com.example.timmy.myapplication.widget.LineChart;

import java.util.ArrayList;

public class LineChartActivity extends AppCompatActivity {

    private LineChart mLineChartView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linchart);

        mLineChartView = (LineChart) findViewById(R.id.lineChart);
        //X轴
        String[] xItem = {"1", "2", "3", "4", "5"};
        ArrayList xItemArray = new ArrayList();
        for (int i = 0; i < xItem.length; i++) {
            xItemArray.add(xItem[i]);
        }
        //Y轴
        int[] yItem = {20, 36, 19, 25, 20, 19, 27, 8, 18, 19, 21, 20, 19, 20, 8, 18, 19, 21, 20, 22, 21, 24, 26, 24, 20, 22, 21, 24, 26, 24};
        ArrayList<Integer> yItemArray = new ArrayList<>();
        for (int i = 0; i < xItem.length; i++) {
            yItemArray.add(yItem[i]);
        }
        int yMax = findMax(yItem);
        mLineChartView.setXItem(xItemArray);
        mLineChartView.setYItem(yItemArray);
        mLineChartView.setMaxYValue(yMax);

    }

    // 获得数组中最大值
    private int findMax(int[] array) {
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) max = array[i];
        }
        return max;
    }
}
